<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8" />
    <script>
        var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
        document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')
    </script>
    <title></title>
    <style>
        html,
        body,
        .container {
            margin: 0;
            padding: 0;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            background: #ffffff;
        }

        #map {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 80px;
            bottom: calc(80px + constant(safe-area-inset-bottom));
            bottom: calc(80px + env(safe-area-inset-bottom));
        }

        #poi {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            bottom: constant(safe-area-inset-bottom);
            bottom: env(safe-area-inset-bottom);
            height: 80px;
            background: #FFFFFF;
        }

        .poi-info {
            width: 100%;
            padding: 23px 16px 23px 18px;
            box-sizing: border-box;
            background: #FFFFFF;
        }

        .poi-name {
            font-size: 17px;
            line-height: 17px;
            color: #111111;
            display: block;
            margin-right: 70px;
            word-wrap: break-word;
        }

        .poi-addr {
            font-size: 13px;
            line-height: 13px;
            color: #666666;
            display: block;
            margin-top: 4px;
            margin-right: 70px;
            word-wrap: break-word;
        }

        .poi-nav {
            display: block;
            position: absolute;
            top: 10px;
            right: 16px;
            width: 60px;
            height: 60px;
            border-radius: 60px;
            overflow: hidden;
        }

        .poi-nav i,
        .poi-nav span {
            position: absolute;
            top: 0;
            width: 100%;
            height: 100%;
            display: inline-block;
            border-radius: 60px;
        }

        .poi-nav i {
            background: url() no-repeat;
            background-size: 100%;
        }
    </style>
</head>

<body>
    <div id="container" class="container">
        <div id="map"></div>
        <div id="poi">
            <div class="poi-info">
                <span class="poi-name"></span>
                <span class="poi-addr"></span>
                <div class="poi-nav">
                    <i></i>
                    <span></span>
                </div>
            </div>
        </div>
    </div>
    <script>
        var loc;
        var serviceWebview;
        var back = function() {
            var webview = plus.webview.currentWebview()
            if (webview.__uniapp_statusbar_style === 'light') {
                plus.navigator.setStatusBarStyle('light')
            }
            webview.close('auto');
        };

        var isIos = !!navigator.userAgent.match(/iPhone|iPad|iPod/i);

        document.addEventListener('plusready', function() {
            serviceWebview = plus.webview.getWebviewById('__W2A_CONTEXT_') || plus.webview.getLaunchWebview();
            plus.key.addEventListener('backbutton', back);


        })

        /**
         * 调用系统第三方程序进行导航
         */
        function openSysMap(lat, lng, title) {
            /**
             * 网页版地图源
             */
            var mapsSourceWeb = [
                {
                    title: '腾讯地图网页版',
                    getUrl: function() {
                        var url
                        url = 'https://apis.map.qq.com/uri/v1/routeplan?type=drive' + '&to=' +
                            encodeURIComponent(title) + '&tocoord=' + encodeURIComponent(lat + ',' + lng) +
                            '&referer=APP'
                        return url
                    }
                }
            ]
            /**
             * APP版地图源
             */
            var mapsSource = [
                {
                    title: '高德地图',
                    pname: 'com.autonavi.minimap',
                    action: !isIos ? 'amapuri://' : 'iosamap://',
                    getUrl: function() {
                        var url
                        if (!isIos) {
                            url = 'amapuri://route/plan/'
                        } else {
                            url = 'iosamap://path'
                        }
                        url += '?sourceApplication=APP&dname=' + encodeURIComponent(title) + '&dlat=' + lat +
                            '&dlon=' + lng + '&dev=0'
                        return url
                    }
                },
                {
                    title: '百度地图',
                    pname: 'com.baidu.BaiduMap',
                    action: 'baidumap://',
                    getUrl: function() {
                        var url = 'baidumap://map/direction?destination=' + encodeURIComponent('latlng:' + lat +
                            ',' + lng + '|name:' + title) + '&mode=driving&src=APP&coord_type=gcj02'
                        return url
                    }
                },
                {
                    title: '腾讯地图',
                    pname: 'com.tencent.map',
                    action: 'qqmap://',
                    getUrl: function() {
                        var url
                        url = 'qqmap://map/routeplan?type=drive' + (isIos ? ('&from=' + encodeURIComponent(
                            '我的位置')) : '') + '&to=' + encodeURIComponent(title) + '&tocoord=' +
                            encodeURIComponent(lat + ',' + lng) + '&referer=APP'
                        return url
                    }
                }
            ]
            var maps = []
            mapsSource.forEach(function(mapsSource) {
                var installed = plus.runtime.isApplicationExist({
                    pname: mapsSource.pname,
                    action: mapsSource.action,
                })
                if (installed) {
                    maps.push(mapsSource)
                }
            })
            if (isIos) {
                maps.unshift({
                    title: 'Apple 地图',
                    getUrl: function() {
                        var url
                        url = 'https://maps.apple.com/?daddr=' + encodeURIComponent(title) + '&sll=' +
                            encodeURIComponent(lat + ',' + lng)
                        return url
                    }
                })
            }
            if (maps.length === 0) {
                maps = maps.concat(mapsSourceWeb)
            }
            plus.nativeUI.actionSheet({
                title: '导航方式',
                cancel: '取消',
                buttons: maps,
            }, function(res) {
                var index = res.index
                var map
                if (index > 0) {
                    map = maps[index - 1]
                    plus.runtime.openURL(map.getUrl(), function() { }, map.pname)
                }
            })
        }

        var ZOOM = 13
        window.__openLocation__ = function(params) {
            var mapElem = document.getElementById('map');
            var poiNameElem = document.querySelector('.poi-name');
            var poiAddrElem = document.querySelector('.poi-addr');
            var poiNavElem = document.querySelector('.poi-nav');
            var latitude = params.latitude;
            var longitude = params.longitude;
            var scale = params.scale;
            var name = params.name;
            var address = params.address;
            var point = new plus.maps.Point(longitude, latitude);
            var map = plus.maps.create('map', {
                center: point,
                zoom: scale || ZOOM,
                top: 0,
                left: 0,
                width: mapElem.offsetWidth,
                height: mapElem.offsetHeight

            });
            var marker = new plus.maps.Marker(point);
            marker.setIcon('__uniappmarker@3x.png');
            if (name) {
                poiNameElem.innerText = name;
                // marker.setLabel(name);
            }
            if (address) {
                poiAddrElem.innerText = address;
                //                             var bubble = new plus.maps.Bubble(address);
                //                             marker.setBubble(bubble);
            }
            map.addOverlay(marker);
            plus.webview.currentWebview().append(map);
            var userPoint = false
            map.getUserLocation(function(state, point) {
                if (state) {
                    plus.nativeUI.toast('定位失败!');
                } else {
                    userPoint = point;
                }
            })

            poiNavElem.addEventListener('click', function() {
                openSysMap(latitude, longitude, name)
            });
        }
    </script>
</body>

</html>